<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            float: left;
            margin:50px;
        }
    </style>
</head>
<body>
    <script>
        var z=0;
        function gu(x,y){
            this.x=x;
            this.y=y;
            this.color='red';
            this.xiang();
            this.rui();
            a.push(this);
        }
        gu.prototype.xiang=function(){
            this.dom=document.createElement('div');
            this.dom.style.width=this.x+'px';
            this.dom.style.height=this.y+'px';
            this.dom.style.backgroundColor=this.color;
            document.body.appendChild(this.dom);
            this.dom.innerHTML=z; 
            z++;  
        }
        var a=[];

        gu.prototype.rui=function(){
            var that=this;
            this.dom.onclick=function(){
                that.g1();
            }
        }
        gu.prototype.g1=function(){
           console.log(this.dom.innerHTML);
        }
        for(var i=0;i<3;i++){
            new gu(100,100);
        }
      
    </script>
</body>
</html>